<template>
  <!-- 整体容器 -->
  <div class="login-container">
    <!-- 左侧盒子 -->
    <div class="left-box">
      <!-- 顶部标题 -->
      <div class="title-box">
        <img src="../../assets/login_logo.png" alt class="logo" />
        <span class="title">黑马面面</span>
        <span class="line"></span>
        <span class="sub-title">用户登录</span>
      </div>
      <!-- 表单 -->
      <el-form ref="loginForm" :rules="rules" :model="loginForm" label-width="43px">
        <!-- 手机号 -->
        <el-form-item prop="phone">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="loginCode">
          <el-row>
            <el-col :span="15">
              <el-input
                prefix-icon="el-icon-key"
                v-model="loginForm.loginCode"
                placeholder="请输入验证码"
              ></el-input>
            </el-col>
            <el-col class="img-box" :offset="2" :span="7">
              <img :src="codeUrl" alt @click="changCodeUrl" />
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 用户协议 -->
        <el-form-item>
          <el-checkbox v-model="loginForm.isCheck">
            我已阅读并同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条框</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="my-btn" type="primary" @click="subitForm('loginForm')">登录</el-button>
          <el-button class="my-btn" type="primary" @click="showRegister">注册</el-button>
        </el-form-item>
      </el-form>
      <!-- 右边盒子 图片 -->
    </div>
    <img src="../../assets/login_banner_ele.png" alt />
    <registerDialog ref="registerDialog"></registerDialog>
  </div>
</template>

<script>
// 导入 注册对话框 组件
import registerDialog from "./components/registerDialog.vue";
// 导入验证 手机 邮箱规则
import { checkPhone } from "@/utils/validator";
// 导入请求登录的方法
import { login } from "@/api/login"
// 导入 token 的处理方法
import { setToken } from "@/utils/token"
export default {
  name: "login",
  //   注册组件
  components: {
    //   es6 语法 省略属性值
    registerDialog
  },
  props: {},
  data () {
    return {
      loginForm: {
        // 手机号
        phone: "15816539339",
        // 密码
        password: "123456",
        // 验证码
        loginCode: "",
        // 是否勾选
        isCheck: true
      },
      rules: {
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 12, message: "密码长度为6-12位", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          { validate: checkPhone, trigger: "blur" }
        ],
        loginCode: [
          { required: true, message: "验证码不能为空", trigger: "blur" },
          { min: 4, max: 4, message: "验证码长度为4位", trigger: "blur" }
        ]
      },
      codeUrl: process.env.VUE_APP_URL + "/captcha?type=login"
    };
  },
  computed: {},
  created () { },
  mounted () { },
  watch: {},
  methods: {
    // 点击刷新验证码
    changCodeUrl () {
      this.codeUrl = this.codeUrl + "&t=" + Date.now()
    },
    // 提交表单
    subitForm (formName) {

      this.$refs[formName].validate(async valid => {
        if (valid) {
          // this.$message.success("验证成功");
          // 验证是否勾选
          if (!this.loginForm.isCheck) {
            return this.$message.warning('请勾选用户协议')
          }
          // 发送请求
          let res = await login({
            phone: this.loginForm.phone,
            password: this.loginForm.password,
            code: this.loginForm.loginCode
          })
          // 根据返回状态码处理
          if (res.data.code == 200) {
            // 保存token
            setToken(res.data.data.token)
            // 跳转到登录页
            this.$router.push('/index/chart')
            this.$message.success('欢迎你')
          } else {
            this.$message.error(res.data.message)
          }
          window.console.log(res)
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
    // 显示对话框
    showRegister () {
      // this.$refs 可以获取所有设置了 ref 属性的元素 包括组件
      // registerDialog 和 上面设置的属性要一致
      // 也可以用 this.$refs['registerDialog']
      this.$refs.registerDialog.dialogFormVisible = true;
    }
  }
};
</script>

<style lang="less">
  .login-container {
    height: 100%;
    background: linear-gradient(
      225deg,
      rgba(20, 147, 250, 1),
      rgba(1, 198, 250, 1)
    );
    /* 开启弹性布局 */
    display: flex;
    /* 上下居中 */
    align-items: center;
    /*左右均分  */
    justify-content: space-around;
    //   左侧盒子
    .left-box {
      width: 478px;
      height: 550px;
      background-color: #f5f5f5;
      padding-right: 41px;
      box-sizing: border-box;
      .title-box {
        //   开启弹性布局
        display: flex;
        align-items: center;
        margin-top: 44px;
        margin-left: 48px;
        margin-bottom: 27px;
        .title {
          font-size: 24px;
          margin-left: 16px;
          margin-right: 14px;
        }
        .line {
          width: 1px;
          height: 28px;
          background-color: #c7c7c7;
        }
        .sub-title {
          font-size: 21px;
          margin-left: 12px;
        }
      }
      // 图片盒子
      .img-box {
        height: 40.8px;
        // 登录验证码图片
        img {
          height: 40.8px;
          width: 100%;
        }
      }
      // 协议区域的布局
      .el-checkbox {
        display: flex;
        .el-checkbox__label {
          display: flex;
        }
      }
      // 底部按钮
      .my-btn {
        width: 100%;
        margin-top: 26px;
        margin-left: 0;
      }
    }
  }
</style>
